package com.itstu.controller;

import com.itstu.annotaion.LoginUser;
import com.itstu.model.AjaxResult;
import com.itstu.pojo.Message;
import com.itstu.pojo.User;
import com.itstu.pojo.dto.MessageDTO;
import com.itstu.service.MessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author 梁卓
 * @Version 1.0
 * @ClassName MessageController
 * @Description TODO
 * @Date 2024/10/09 21:03
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageService messageService;

    /**
     * 获取消息列表
     * @param user 登录用户信息
     * @param to 好友id
     * @return 消息列表
     */
    @GetMapping
    public AjaxResult getMessageList(@LoginUser User user, Long to) {
        List<MessageDTO> resultList = messageService.getMessageList(user.getId(), to);
        return AjaxResult.success(resultList);
    }

    /**
     * 发送消息
     * @param message 消息内容
     * @return 发送结果
     */
    @PostMapping
    public AjaxResult sendMessage(@LoginUser User user, @RequestBody Message message) {
        message.setFrom(user.getId());
        message.setType(1);
        message.setTime(new Date());
        messageService.sendMessage(message);
        return AjaxResult.success();
    }

}
